<template>
  <div class="announcement-detail-container">
    <el-card>
      <div class="announcement-header">
        <h1>{{ announcement.title }}</h1>
        <div class="announcement-meta">
          <el-tag :type="getCategoryType(announcement.category)">
            {{ announcement.category }}
          </el-tag>
          <span class="publish-time">发布时间：{{ formatDate(announcement.createdAt) }}</span>
        </div>
      </div>

      <div class="announcement-content" v-html="announcement.content"></div>

      <div class="actions">
        <el-button @click="$router.push('/announcements')">返回列表</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

export default {
  name: 'AnnouncementDetail',
  setup() {
    const route = useRoute()
    const store = useStore()
    const announcement = ref({})

    const loadAnnouncementDetail = async () => {
      try {
        const response = await store.dispatch('fetchAnnouncementDetail', route.params.id)
        announcement.value = response.data
      } catch (error) {
        ElMessage.error('获取公告详情失败')
      }
    }

    const formatDate = (date) => {
      return dayjs(date).format('YYYY-MM-DD HH:mm')
    }

    const getCategoryType = (category) => {
      const types = {
        'SYSTEM_UPDATE': 'info',
        'EVENT_NOTICE': 'success',
        'IMPORTANT': 'warning'
      }
      return types[category] || 'info'
    }

    onMounted(() => {
      loadAnnouncementDetail()
    })

    return {
      announcement,
      formatDate,
      getCategoryType
    }
  }
}
</script>

<style scoped>
.announcement-detail-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.announcement-header {
  margin-bottom: 30px;
}

h1 {
  margin: 0 0 15px 0;
  font-size: 24px;
  color: #303133;
}

.announcement-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  color: #909399;
}

.publish-time {
  font-size: 14px;
}

.announcement-content {
  line-height: 1.8;
  color: #303133;
  margin: 20px 0;
}

.actions {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}
</style>
